package com.isoft.mapper;

import com.isoft.entity.BaseCfieEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface BaseCfieMapper {
    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) VALUES(#{entity.meta.sender,jdbcType=VARCHAR}, #{entity.meta.receiver,jdbcType=VARCHAR}, #{entity.meta.sequenceNumber,jdbcType=NUMERIC}, TO_TIMESTAMP(#{entity.meta.dateTime,jdbcType=VARCHAR}, 'YYYYMMDDHH24MISS'), #{entity.meta.type,jdbcType=VARCHAR}, #{entity.meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "entity.meta.msgId")
    void insertMeta(@Param("entity") BaseCfieEntity entity);

    @Select("SELECT SEQ_FLIGHT_ID.NEXTVAL FROM DUAL")
    Long getNextFlightId();

    @Insert("INSERT INTO FLIGHT_BASE(FLIGHT_ID) VALUES(#{flightId})")
    void insertDummyFlight(Long flightId);

    @Insert("MERGE INTO AIRCRAFT USING (SELECT #{flightId} AS FLIGHT_ID, #{aircraftType} AS AIRCRAFT_TYPE, #{aircraftNumber} AS AIRCRAFT_NUMBER FROM DUAL) S ON (AIRCRAFT.AIRCRAFT_NUMBER = S.AIRCRAFT_NUMBER) WHEN NOT MATCHED THEN INSERT (FLIGHT_ID, AIRCRAFT_TYPE, AIRCRAFT_NUMBER) VALUES (#{flightId}, #{aircraftType}, #{aircraftNumber})")
    void insertAircraftWithFlight(Long flightId, String aircraftType, String aircraftNumber);

    @Insert("INSERT INTO FLIGHT_IRREGULARITY(FLIGHT_ID) VALUES(#{flightId})")
    void insertIrregularity(Long flightId);
} 